/*
	DHL_PsiTypes.h

	PHOENIX Driver HAL library
	
	PSI/PSIP API implementation

	Digital STREAM Technology, Inc.
*/


#ifndef __DHL_PSI_TYPES_H__
#define __DHL_PSI_TYPES_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "DHL_MPEGTypes.h"



/*******************************************************************************
 * MPEG_SI.h - MPEG System Information (SI) parsing definitions 
 *
 * Copyright (c) 2004 Zoran, Corporation.  All Rights Reserved Worldwide.
 *
 * THIS PROGRAM IS CONFIDENTIAL AND PROPRIETARY TO ZORAN, CORP. AND
 * CONSTITUTES A VALUABLE TRADE SECRET. Any unauthorized use,
 * reproduction, modification, or disclosure of this program is strictly
 * prohibited.	Any use of this program by an authorized licensee is
 * strictly subject to the terms and conditions, including
 * confidentiality obligations, set forth in the applicable Software
 * License Agreement between Zoran, Corp. and the licensee.
 *
 * $Id: //suprahd/releases/suprahd_239/suprahd_zTVApp660_239/include/MPEG_SI.h#1 $
 * $Revision: #1 $
 * $DateTime: 2006/06/05 14:53:58 $
 * $Change: 45890 $
 * $Author: catherine.lee $
 * 
 ******************************************************************************/

/* macros */
#define get_table_id(p)				(p[0])
#define get_section_syntax_indicator(p)	(p[1] >> 7)
#define get_private_indicator(p)		((p[1] >> 6) & 0x1)
#define get_section_length(p)			(((p[1] & 0x0F) << 8) | p[2])
#define get_section_number(p)			(p[6])
#define get_last_section_number(p)		(p[7])

#define checkMemoryError(p)			if (p == NULL) {err = DHL_FAIL_OUT_OF_MEMORY; goto ParseExit;}
#define checkBitBufError(b)			if (bitBufferCheckError(b)) {err = DHL_FAIL_BAD_FORMAT; goto ParseExit; }

/*
 * Assigned PID's
 */
#define PAT_PID 0x0000
#define EIT_PID 0x0012
#define DIT_PID 0x001E
#define SIT_PID 0x001F

/*
 * Table ID's
 */
#define PROGRAM_ASSOCIATION_SECTION	0
#define CA_SECTION 1
#define TS_PROGRAM_MAP_SECTION 2
#define DIT_SECTION 0x7E
#define SIT_SECTION 0x7F

/***************************** PAT ********************************/

/*
 * MPEG descriptor types (Table 2-39)
 */
#define video_stream_tag 					0x02
#define audio_stream_tag 					0x03
#define hierarchy_tag 						0x04
#define registration_tag 		  			0x05
#define data_stream_alignment_tag 			0x06
#define target_background_grid_tag 			0x07
#define video_window_tag 					0x08
#define CA_tag 								0x09
#define ISO_639_language_tag 				0x0A
#define system_clock_tag 					0x0B
#define multiplex_buffer_utilization_tag 	0x0C
#define copyright_tag 						0x0D
#define maximum_bitrate_tag 				0x0E
#define private_data_indicator_tag 			0x0F
#define smoothing_buffer_tag 				0x10
#define STD_tag 							0x11
#define IBP_tag 							0x12

/*
 * ATSC descriptor types
 */
#define ATSC_stuffing_tag					0x80
#define AC3_audio_stream_tag				0x81
#define program_identifier_tag				0x85
#define caption_service_tag					0x86
#define content_advisory_tag				0x87
#define extended_channel_name_tag			0xA0
#define service_location_tag				0xA1
#define ATSC_time_shifted_service_tag 		0xA2
#define component_name_tag					0xA3
#define dcc_departing_request_tag  			0xA8
#define dcc_arriving_request_tag       		0xA9
#define redistribution_control_tag       	0xAA

/*
 * MPEG stream types in the PMT (Table 2-29)
 * NOT to be confused with stream ID's found in PES headers
 */
#define StreamType_MPEG1Video	1
#define StreamType_MPEG2Video	2
#define StreamType_MPEG1Audio	3
#define StreamType_MPEG2Audio	4
#define StreamType_MPEG2PrivateSection	5
#define StreamType_MPEG2PESPrivateData	6
#define StreamType_MHEG			7
#define StreamType_AnnexADSMCC	8
#define StreamType_H222_1		9
#define StreamType_DSMCCTypeA	10
#define StreamType_DSMCCTypeB	11
#define StreamType_DSMCCTypeC	12
#define StreamType_DSMCCTypeD	13
#define StreamType_MPEG2Auxiliary 14
#define StreamType_AACAudio		15
#define StreamType_DC2Video		0x80
#define StreamType_AC3Audio		0x81

#define StreamTypeString(t) ( \
	(t)==StreamType_MPEG1Video ? "MPEG1Video" : \
	(t)==StreamType_MPEG2Video ? "MPEG2Video" : \
	(t)==StreamType_MPEG1Audio ? "MPEG1Audio" : \
	(t)==StreamType_MPEG2Audio ? "MPEG2Audio" : \
	(t)==StreamType_MPEG2PrivateSection ? "MPEG2PrivSect" : \
	(t)==StreamType_MPEG2PESPrivateData ? "MPEG2PESPrivate" : \
	(t)==StreamType_MHEG ? "MHEG" : \
	(t)==StreamType_AnnexADSMCC ? "AnxADSMCC" : \
	(t)==StreamType_H222_1 ? "H222.1" : \
	(t)==StreamType_DSMCCTypeA ? "DSMCC_A" : \
	(t)==StreamType_DSMCCTypeB ? "DSMCC_B" : \
	(t)==StreamType_DSMCCTypeC ? "DSMCC_C" : \
	(t)==StreamType_DSMCCTypeD ? "DSMCC_D" : \
	(t)==StreamType_MPEG2Auxiliary ? "MPEG2Aux" : \
	(t)==StreamType_AACAudio ? "AACAudio" : \
	(t)==StreamType_DC2Video ? "DC2Video" : \
	(t)==StreamType_AC3Audio ? "AC3Audio" : \
	"?")

/*
 * This represents one program within the Program Association Table
 */
typedef struct MPEG_PAT_program {
	UINT16		program_number;
	UINT16		program_map_PID;
} MPEG_PAT_program;

/*
 * This contains all interesting fieldsin a Program Association Table.
 * All sections in a multi-section PAT are included in this
 * one structure.
 */
typedef struct MPEG_PAT {
	UINT16				transport_stream_id;
	UINT8				version_number;
	BOOLEAN				current_next_indicator;
	UINT8				section_number;
	UINT8				last_section_number;
	BOOLEAN				isWholePAT;				/* FALSE if only a singe section of a PAT */
	UINT16				numPrograms;
	BOOLEAN				networkPIDPresent;		/* TRUE if network_pid is valid */
	UINT16				network_PID;
	MPEG_PAT_program	*programs;
} MPEG_PAT;



/***************************** PMT ********************************/

/* video_stream_descriptor */
typedef struct VideoStreamDescriptor {
	BOOLEAN				multiple_frame_rate_flag;
	MPEG_FRAME_RATE		frame_rate_code;
	BOOLEAN				MPEG_1_only_flag;
	BOOLEAN				constrained_parameter_flag;
	BOOLEAN				still_picture_flag;
	UINT8				profile_and_level_indication;
	MPEG_CHROMA_FORMAT 	chroma_format;
	BOOLEAN				frame_rate_extension_flag;
} VideoStreamDescriptor;

/* Video Decode Control Descriptor (ARIB only) */
typedef struct VideoDecodeControlDescriptor {
	BOOLEAN				valid;					/* TRUE if this descriptor exists in PMT */
	BOOLEAN				still_picture_flag;
	BOOLEAN				sequence_end_code_flag;
	UINT8				video_decode_format;
} VideoDecodeControlDescriptor;

typedef struct MPEG_PMT_stream {
	UINT8	stream_type;
	UINT16	elementary_PID;
	UINT16	descriptor_length;
	UINT8	*descriptors;
	VideoStreamDescriptor *videoStreamDescriptor;	/* NULL if not present */
	VideoDecodeControlDescriptor videoDecodeControlDescriptor;	/* Not a pointer. Check valid flag */
} MPEG_PMT_stream;

/*
 * This contains all interesting fields in a Program Mapping Table
 */
#define DYNARRAY_PMTStream	32		/* Debugger thinks there's this many streams - Does not specify actual length of structure */
typedef struct MPEG_PMT {
	UINT16	PID;
	UINT16	program_number;
	UINT8	version_number;
	BOOLEAN	current_next_indicator;
	UINT16	PCR_PID;
	UINT16	descriptor_length;
	UINT8	*descriptors;		/* program descriptors */
	int		numStreams;
	MPEG_PMT_stream	streams[DYNARRAY_PMTStream];
} MPEG_PMT;
#define MPEG_PMTSize(nelem) (sizeof(MPEG_PMT) + \
			((nelem)-DYNARRAY_PMTStream)*sizeof(MPEG_PMT_stream))




/********************************************************************
 *
 * ATSC_PSIP.h - ATSC A/65 Program and System Information Protocol (PSIP). 
 *
 * Copyright 1997-2002 TeraLogic, Inc.
 * All Rights Reserved
 *
 * $Id: //suprahd/releases/suprahd_239/suprahd_zTVApp660_239/include/ATSC_PSIP.h#1 $
 * $Revision: #1 $
 * $DateTime: 2006/06/05 14:53:58 $
 * $Change: 45890 $
 * $Author: catherine.lee $
 * 
 ********************************************************************/

 

/* definitions */
#define PSIP_PROTOCOL_VERSION		0x00
#define PSIP_BASE_PID				0x1FFB



/*==============================================================================
	 ATSC Stream Types
==============================================================================*/
typedef enum {
	atsc_a53_audio			= 0x81,
	atsc_program_identifier		= 0x85
} ATSC_stream_type_k;


/*==============================================================================
	 ATSC Format Identifiers (for registration_descriptor)
==============================================================================*/
typedef enum {
	atsc_format_identifier		= 0x47413934,
	ac3_format_identifier		= 0x41432D33
} ATSC_format_identifier_k;


/*==============================================================================
	 Multiple String Structure
==============================================================================*/

/* Multiple String Structure */
typedef enum {
	cm_None			= 0x00,
	cm_Huffman_C4C5		= 0x01,
	cm_Huffman_C6C7		= 0x02
} compress_type_k;

typedef enum {
	tm_ISO10646_1_Page_0x00	= 0x00,
	tm_ISO10646_1_Page_0x01	= 0x01,
	tm_ISO10646_1_Page_0x02	= 0x02,
	tm_ISO10646_1_Page_0x03	= 0x03,
	tm_ISO10646_1_Page_0x04	= 0x04,
	tm_ISO10646_1_Page_0x05	= 0x05,
	tm_ISO10646_1_Page_0x06	= 0x06,
	/* 0x07-0x08 Reserved */
	tm_ISO10646_1_Page_0x09	= 0x09,
	tm_ISO10646_1_Page_0x0A	= 0x0A,
	tm_ISO10646_1_Page_0x0B	= 0x0B,
	tm_ISO10646_1_Page_0x0C	= 0x0C,
	tm_ISO10646_1_Page_0x0D	= 0x0D,
	tm_ISO10646_1_Page_0x0E	= 0x0E,
	/* 0x0F Reserved */
	tm_ISO10646_1_Page_0x10	= 0x10,
	/* 0x11-0x1F Reserved */
	tm_ISO10646_1_Page_0x20	= 0x20,
	tm_ISO10646_1_Page_0x21	= 0x21,
	tm_ISO10646_1_Page_0x22	= 0x22,
	tm_ISO10646_1_Page_0x23	= 0x23,
	tm_ISO10646_1_Page_0x24	= 0x24,
	tm_ISO10646_1_Page_0x25	= 0x25,
	tm_ISO10646_1_Page_0x26	= 0x26,
	tm_ISO10646_1_Page_0x27	= 0x27,
	/* 0x28-0x2F Reserved */
	tm_ISO10646_1_Page_0x30	= 0x30,
	tm_ISO10646_1_Page_0x31	= 0x31,
	tm_ISO10646_1_Page_0x32	= 0x32,
	tm_ISO10646_1_Page_0x33	= 0x33,
	/* 0x34-0x3E Reserved */
	tm_ISO10646_1_16_bit	= 0x3F,
	/* 0x40-0xDF Reserved */
	/* 0xE0-0xFE Private */
	tm_Not_applicable		= 0xFF
} text_mode_k;

typedef struct msSegment {
	compress_type_k		compression_type;
	text_mode_k			mode;
	UINT8				number_bytes;
	UINT8*			compress_string_byte;
} msSegment_t, *msSegmentPtr_t;

typedef struct msString {
	UINT32			ISO639_language_code;
	UINT8				number_segments;
	msSegmentPtr_t		segment;			/* array of size 'number_segments' */
} msString_t, *msStringPtr_t;

typedef struct multipleString {
	UINT8				number_strings;
	msStringPtr_t		msString;			/* array of size 'number_strings' */
} multipleString_t, *multipleStringPtr_t;



/*==============================================================================
	 PSIP Table IDs
==============================================================================*/
typedef enum {
	tid_master_guide_table		= 0xC7,
	tid_terr_virtual_chan_table	= 0xC8,
	tid_cable_virtual_chan_table	= 0xC9,
	tid_rating_region_table		= 0xCA,
	tid_event_information_table	= 0xCB,
	tid_extended_text_table		= 0xCC,
	tid_system_time_table		= 0xCD,
	tid_directed_channel_change_table = 0xD3,
	tid_dcc_selection_code_table = 0xD4,
	tid_emergency_alert_service_table = 0xD8
} table_id_k;



/*==============================================================================
	 PSIP Tables
==============================================================================*/

/* System Time Table (STT) */
typedef struct daylightSavings {
	UINT8				DS_status;
	UINT8				DS_day_of_month;
	UINT8				DS_hour;
} daylightSavings_t, *daylightSavingsPtr_t;

typedef struct sttSection {
	UINT32			system_time;
	UINT8				GPS_UTC_offset;
	daylightSavings_t		daylight_savings;
	UINT16			descriptor_length;
	UINT8*			descriptors;		/* un-parsed descriptors */
} sttSection_t, *sttSectionPtr_t;


/* Master Guide Table (MGT) */
typedef enum {
	tt_terrestrial_VCT_cni_1	= 0x0000,
	tt_terrestrial_VCT_cni_0	= 0x0001,
	tt_cable_VCT_cni_1			= 0x0002, /* same for LVCT in OOB */
	tt_cable_VCT_cni_0			= 0x0003, /* same for LVCT in OOB */
	tt_channel_ETT			= 0x0004,
	/* 0x0005 - 0x0006 reserved */
	tt_SVCT_VCM                 = 0x0010,   
	tt_SVCT_DCM     		    = 0x0011,   
	tt_SVCT_ICM       			= 0x0012,   
	/* 0x0013 - 0x001F Reserved */
	tt_NIT_CDS					= 0x0020,
	tt_NIT_MMS					= 0x0021,
	/* 0x0022 - 0x002F reserved */
	tt_NTT_SNS					= 0x0030,
	/* 0x0031 - 0x00FF reserved */
	tt_EIT_min				= 0x0100,
	tt_EIT_max				= 0x017F,
	/* 0x0180-0x01FF Reserved */
	tt_event_ETT_min			= 0x0200,
	tt_event_ETT_max			= 0x027F,
	/* 0x0280-0x0300 Reserved */
	tt_RRT_region_1			= 0x0301,
	tt_RRT_region_255			= 0x03FF,
	/* 0x0400-0x0FFF User Private */
	tt_AEIT_min 				= 0x1000,
	tt_AEIT_max					= 0x10FF,
	tt_AETT_min 				= 0x1100,
	tt_AETT_max					= 0x11FF
	/* 0x1200-0xFFFF Reserved */
} table_type_k;

typedef struct mgtTable {
	table_type_k		table_type;
	UINT16			table_type_PID;
	UINT8				table_type_version_number;
	UINT32			number_bytes;
	UINT16			descriptor_length;
	UINT8*			descriptors;		/* un-parsed descriptors */
} mgtTable_t, *mgtTablePtr_t;

typedef struct mgtSection {
	UINT16                  map_id; /* only used in OOB */
	UINT8				version_number;
	UINT16			tables_defined;
	mgtTablePtr_t		table;			/* array of size 'tables_defined' */
	UINT16			descriptor_length;
	UINT8*			descriptors;		/* un-parsed descriptors */
} mgtSection_t, *mgtSectionPtr_t;


/* Terrestrial Virtual Channel Table (TVCT) */
typedef enum {
	mm_analog			= 0x01,
	mm_SCTE_mode_1		= 0x02,
	mm_SCTE_mode_2		= 0x03,
	mm_ATSC_8VSB		= 0x04,
	mm_ATSC_16VSB		= 0x05,
	/* 0x06-0x7F reserved */
	mm_private_descriptor	= 0x80
	/* 0x81-0xFF private */
} modulation_mode_k;
	
typedef enum {
	ETM_none			= 0x00,
	ETM_in_this_PTC		= 0x01,
	ETM_in_channel_TSID_PTC	= 0x02
} ETM_location_k;

typedef enum {
	/* 0x00 reserved */
	st_analog_television			= 0x01,
	st_ATSC_digital_television		= 0x02,
	st_ATSC_audio_only			= 0x03,
	st_ATSC_data_broadcast_service	= 0x04
	/* 0x05-0x3F reserved */
} service_type_k;

typedef struct tvctChannel {
	SINT16			short_name[7];
	UINT16			major_channel_number;
	UINT16			minor_channel_number;
	modulation_mode_k		modulation_mode;
	UINT32			carrier_frequency;
	UINT16			channel_TSID;
	UINT16			program_number;
	ETM_location_k		ETM_location;
	BOOLEAN			access_controlled;
	BOOLEAN			hidden;
	BOOLEAN			show_guide;
	service_type_k		service_type;
	UINT16			source_id;
	UINT16			descriptor_length;
	UINT8*			descriptors;		/* un-parsed descriptors */
} tvctChannel_t, *tvctChannelPtr_t;

typedef struct tvctSection {
	UINT16			transport_stream_id;
	UINT8				version_number;
	UINT8				current_next_indicator;
	UINT8				section_number;
	UINT8				last_section_number;
	UINT8				num_channels_in_section;
	tvctChannelPtr_t		channel;
	UINT16			additional_descriptor_length;
	UINT8*			additional_descriptors;	/* un-parsed descriptors */
} tvctSection_t, *tvctSectionPtr_t;


typedef struct tvct {
	UINT16			transport_stream_id;
	UINT8				version_number;
	UINT16				numChannels;
	tvctChannelPtr_t		channel;
	UINT16			additional_descriptor_length;
	UINT8*			additional_descriptors;	/* un-parsed descriptors */
} tvct_t, *tvctPtr_t;


/* Cable Virtual Channel Table (CVCT) */
typedef enum {
	ps_path1		= 0,
	ps_path2		= 1
} path_select_k;

typedef struct cvctChannel {
	UINT16			short_name[7];
	UINT16			major_channel_number;
	UINT16			minor_channel_number;
	modulation_mode_k		modulation_mode;
	UINT32			carrier_frequency;
	UINT16			channel_TSID;
	UINT16			program_number;
	ETM_location_k		ETM_location;
	BOOLEAN			access_controlled;
	BOOLEAN			hidden;
	path_select_k		path_select;
	BOOLEAN			out_of_band;
	BOOLEAN			show_guide;
	service_type_k		service_type;
	UINT16			source_id;
	UINT16			descriptor_length;
	UINT8*			descriptors;		/* un-parsed descriptors */
} cvctChannel_t, *cvctChannelPtr_t;

typedef struct cvctSection {
	UINT16			transport_stream_id; /* for oob it is taken as map id */
	UINT8				version_number;
	UINT8				current_next_indicator;
	UINT8				section_number;
	UINT8				last_section_number;
	UINT8				num_channels_in_section;
	cvctChannelPtr_t		channel;
	UINT16			additional_descriptor_length;
	UINT8*			additional_descriptors;	/* un-parsed descriptors */
} cvctSection_t, *cvctSectionPtr_t;


typedef struct cvct {
	UINT16			transport_stream_id;
	UINT8				version_number;
	UINT16				numChannels;
	cvctChannelPtr_t		channel;
	UINT16			additional_descriptor_length;
	UINT8*			additional_descriptors;	/* un-parsed descriptors */
} cvct_t, *cvctPtr_t;


/* Rating Region Table (RRT) */
typedef enum {
	rr_Forbidden	= 0x00,
	rr_United_States	= 0x01,
	rr_Canada			= 0x02
	/* 0x03-0xFF reserved */
} rating_region_k;

typedef struct rrtValue {
	
#if SUPPORT_DST_PLATFORM // cafrii 060306 add
	BOOLEAN block_on;
#endif
	UINT16			abbrev_rating_value_length;
	UINT8				*abbrev_rating_value;
	UINT16			rating_value_length;
	UINT8				*rating_value;
} rrtValue_t, *rrtValuePtr_t;
	
typedef struct rrtDimension {
	UINT16			dimension_name_length;
	UINT8				*dimension_name;
	BOOLEAN			graduated_scale;
#if SUPPORT_DST_PLATFORM // cafrii 060306 add
	BOOLEAN first_value_empty;
#endif
	UINT8				values_defined;
	rrtValuePtr_t		value;			/* array of size 'values_defined' */
} rrtDimension_t, *rrtDimensionPtr_t;

typedef struct rrtSection {
	rating_region_k		rating_region;
	UINT8				version_number;
	UINT16			rating_region_name_length;
	UINT8				*rating_region_name;
	UINT8				dimensions_defined;
	rrtDimensionPtr_t		dimension;			/* array of size 'dimensions_defined' */
	UINT16			descriptor_length;
	UINT8*			descriptors;		/* un-parsed descriptors */
} rrtSection_t, *rrtSectionPtr_t;


/* Event Information Table (EIT) */
typedef struct eitEvent {
	UINT16			event_id;
	UINT32			start_time;
	ETM_location_k		ETM_location;
	UINT32			length_in_seconds;
	UINT16			title_length;
	UINT8				*title;
	UINT16			descriptor_length;
	UINT8*			descriptors;		/* un-parsed descriptors */
} eitEvent_t, *eitEventPtr_t;

typedef struct eitSection {
	UINT16			source_id;
	UINT8				version_number;
	UINT8				section_number;
	UINT8				last_section_number;
	UINT8				num_events_in_section;
	eitEventPtr_t		event;			/* array of size 'num_events_in_section' */
} eitSection_t, *eitSectionPtr_t;

typedef struct eit {
	UINT16			source_id;
	UINT8				version_number;
	UINT16				numEvents;
	eitEventPtr_t		event;			/* array of size 'numEvents' */
} eit_t, *eitPtr_t;


/* Extended Text Table (ETT) */
typedef struct ett {
	UINT8				version_number;
	UINT32			ETM_id;
	UINT16			extended_text_message_length;
	UINT8				*extended_text_message;
} ettSection_t, *ettSectionPtr_t;

/*
 * Emergency Alert Services - [Refer SCTE 18] 
 *
 */

typedef struct locationCode{
	UINT8	state_code;
	UINT8	country_sub_division;
	UINT16	country_code;
} locationCode_t, *locationCodePtr_t;

typedef struct exceptionCount{
	UINT8	in_band_reference;
	UINT16	exception_major_channel_number;
	UINT16  exception_minor_channel_number;
	UINT16	exception_oob_source_id;
} exceptionCount_t, *exceptionCountPtr_t;


typedef struct easSection {

	UINT8				sequence_number;
	
	UINT16				eas_event_id;
	UINT32				eas_originator_code;
	
	UINT8				eas_event_code_length;
	UINT8				*eas_event_code;
	
	UINT8				nature_of_activation_text_length;
	UINT8				*nature_of_activation_text;
	
	UINT8				alert_message_time_remaining;
	UINT32				event_start_time;
	UINT16				event_duration;
	UINT8				alert_priority;
	UINT16				details_oob_source_id;
	UINT16				details_major_channel_number;
	UINT16				details_minor_channel_number;
	UINT16				audio_oob_source_id;
	
	UINT16				alert_text_length;
	UINT8				*alert_text;
	
	UINT8				location_code_count;
	locationCodePtr_t	locationCodeInfo;
	
	UINT8				exception_count;
	exceptionCountPtr_t exceptionInfo;	
	
	UINT16				descriptor_length;
	UINT8*				descriptors;		/* un-parsed descriptors */
} easSection_t, *easSectionPtr_t;


/*==============================================================================
	 PSIP Descriptors
==============================================================================*/

/* AC-3 Audio Descriptor */
typedef enum {
	sr_48kHz				= 0x0,
	sr_44kHz				= 0x1,
	sr_32kHz				= 0x2,
	/* 0x3 reserved */
	sr_48kHz_or_44kHz			= 0x4,
	sr_48kHz_or_32kHz			= 0x5,
	sr_44kHz_or_32kHz			= 0x6,
	sr_48kHz_or_44kHz_or_32kHz	= 0x7
} sample_rate_code_k;

typedef enum {
	br_32kbps			= 0x00,	br_le_32kbps		= 0x20,
	br_40kbps			= 0x01,	br_le_40kbps		= 0x21,
	br_48kbps			= 0x02,	br_le_48kbps		= 0x22,
	br_56kbps			= 0x03,	br_le_56kbps		= 0x23,
	br_64kbps			= 0x04,	br_le_64kbps		= 0x24,
	br_80kbps			= 0x05,	br_le_80kbps		= 0x25,
	br_96kbps			= 0x06,	br_le_96kbps		= 0x26,
	br_112kbps			= 0x07,	br_le_112kbps		= 0x27,
	br_128kbps			= 0x08,	br_le_128kbps		= 0x28,
	br_160kbps			= 0x09,	br_le_160kbps		= 0x29,
	br_192kbps			= 0x0A,	br_le_192kbps		= 0x2A,
	br_224kbps			= 0x0B,	br_le_224kbps		= 0x2B,
	br_256kbps			= 0x0C,	br_le_256kbps		= 0x2C,
	br_320kbps			= 0x0D,	br_le_320kbps		= 0x2D,
	br_384kbps			= 0x0E,	br_le_384kbps		= 0x2E,
	br_448kbps			= 0x0F,	br_le_448kbps		= 0x2F,
	br_512kbps			= 0x10,	br_le_512kbps		= 0x30,
	br_576kbps			= 0x11,	br_le_576kbps		= 0x31,
	br_640kbps			= 0x12,	br_le_640kbps		= 0x32
} bit_rate_code_k;

typedef enum {
	sm_not_indicated		= 0x0,
	sm_not_Dolby_Surround	= 0x1,
	sm_Dolby_Surround		= 0x2
	/* 0x3 reserved */
} surround_mode_k;

typedef enum {
	bm_main_audio_serv_CM	= 0x0,	/* Complete Main */
	bm_main_audio_serv_ME	= 0x1,	/* Music and Effects */
	bm_associated_serv_VI	= 0x2,	/* Visually Impaired */
	bm_associated_serv_HI	= 0x3,	/* Hearing Impaired */
	bm_associated_serv_D	= 0x4,	/* Dialog */
	bm_associated_serv_C	= 0x5,	/* Commentary */
	bm_associated_serv_E	= 0x6,	/* Emergency */
	bm_as_VO_or_ma_K		= 0x7		/* Voice Over (acmod=001) or Karaoke (acmod=010-111) */
} bit_stream_mode_k;

typedef enum {
	nc_acmod_1_1		= 0x0,
	nc_acmod_1_0		= 0x1,
	nc_acmod_2_0		= 0x2,
	nc_acmod_3_0		= 0x3,
	nc_acmod_2_1		= 0x4,
	nc_acmod_3_1		= 0x5,
	nc_acmod_2_2		= 0x6,
	nc_acmod_3_2		= 0x7,
	nc_encoded_1		= 0x8,
	nc_encoded_le_2		= 0x9,
	nc_encoded_le_3		= 0xA,
	nc_encoded_le_4		= 0xB,
	nc_encoded_le_5		= 0xC,
	nc_encoded_le_6		= 0xD
	/* 0xE-0xF reserved */
} num_channels_k;

typedef enum {
	lc_unknown		= 0x00,	lc_Polish		= 0x20,	lc_background	= 0x40,	lc_Moldavian	= 0x60,
	lc_Albanian		= 0x01,	lc_Portuguese	= 0x21,						lc_Malaysian	= 0x61,
	lc_Breton		= 0x02,	lc_Romanian		= 0x22,						lc_Malagasay	= 0x62,
	lc_Catalan		= 0x03,	lc_Romansh		= 0x23,						lc_Macedonian	= 0x63,
	lc_Croatian		= 0x04,	lc_Serbian		= 0x24,						lc_Laotian		= 0x64,
	lc_Welsh		= 0x05,	lc_Slovak		= 0x25,	lc_Zulu		= 0x45,	lc_Korean		= 0x65,
	lc_Czech		= 0x06,	lc_Slovene		= 0x26,	lc_Vietnamese	= 0x46,	lc_Khmer		= 0x66,
	lc_Danish		= 0x07,	lc_Finnish		= 0x27,	lc_Uzbek		= 0x47,	lc_Kazakh		= 0x67,
	lc_German		= 0x08,	lc_Swedish		= 0x28,	lc_Urdu		= 0x48,	lc_Kannada		= 0x68,
	lc_English		= 0x09,	lc_Turkish		= 0x29,	lc_Ukrainian	= 0x49,	lc_Japanese		= 0x69,
	lc_Spanish		= 0x0A,	lc_Flemish		= 0x2A,	lc_Thai		= 0x4A,	lc_Indonesian	= 0x6A,
	lc_Esperanto	= 0x0B,	lc_Walloon		= 0x2B,	lc_Telugu		= 0x4B,	lc_Hindi		= 0x6B,
	lc_Estonian		= 0x0C,						lc_Tatar		= 0x4C,	lc_Hebrew		= 0x6C,
	lc_Basque		= 0x0D,						lc_Tamil		= 0x4D,	lc_Hausa		= 0x6D,
	lc_Faroese		= 0x0E,						lc_Tadzhik		= 0x4E,	lc_Gurani		= 0x6E,
	lc_French		= 0x0F,						lc_Swahili		= 0x4F,	lc_Gujurati		= 0x6F,
	lc_Frisian		= 0x10,						lc_Sranan_Tongo	= 0x50,	lc_Greek		= 0x70,
	lc_Irish		= 0x11,						lc_Somali		= 0x51,	lc_Georgian		= 0x71,
	lc_Gaelic		= 0x12,						lc_Sinhalese	= 0x52,	lc_Fulani		= 0x72,
	lc_Galician		= 0x13,						lc_Shona		= 0x53,	lc_Dari		= 0x73,
	lc_Icelandic	= 0x14,						lc_Serbo_Croat	= 0x54,	lc_Churash		= 0x74,
	lc_Italian		= 0x15,						lc_Ruthenian	= 0x55,	lc_Chinese		= 0x75,
	lc_Lappish		= 0x16,						lc_Russian		= 0x56,	lc_Burmese		= 0x76,
	lc_Latin		= 0x17,						lc_Quechua		= 0x57,	lc_Bulgarian	= 0x77,
	lc_Latvian		= 0x18,						lc_Pustu		= 0x58,	lc_Bengali		= 0x78,
	lc_Luxembourgian	= 0x19,						lc_Punjabi		= 0x59,	lc_Belorussian	= 0x79,
	lc_Lithuanian	= 0x1A,						lc_Persian		= 0x5A,	lc_Bambora		= 0x7A,
	lc_Hungarian	= 0x1B,						lc_Papamiento	= 0x5B,	lc_Azerbijani	= 0x7B,
	lc_Maltese		= 0x1C,						lc_Oriya		= 0x5C,	lc_Assamese		= 0x7C,
	lc_Dutch		= 0x1D,						lc_Nepali		= 0x5D,	lc_Armenian		= 0x7D,
	lc_Norwegian	= 0x1E,						lc_Ndebele		= 0x5E,	lc_Arabic		= 0x7E,
	lc_Occitan		= 0x1F,						lc_Marathi		= 0x5F,	lc_Amharic		= 0x7F
} language_code_k;

typedef enum {
	tc_UNICODE_2_Byte		= 0,
	tc_ISO8859_1		= 1
} text_code_k;

typedef struct ac3AudioStreamDescriptor {
	sample_rate_code_k	sample_rate_code;
	UINT8				bsid;
	bit_rate_code_k		bit_rate_code;
	surround_mode_k		surround_mode;
	bit_stream_mode_k		bsmod;
	num_channels_k		num_channels;
	BOOLEAN			full_svc;
	BOOLEAN			additional_elements1;	/* indicates that more elements are present */
	language_code_k		langcod;
	language_code_k		langcod2;		/* present if 1+1 mode */
	BOOLEAN			additional_elements2;	/* indicates that more elements are present */
	union {
		UINT8			mainid;		
		UINT8			asvcflags;
	} bsmod_ext;
	UINT8				textlen;		/* always zero for ATSC */
	text_code_k			text_code;
	UINT8*			text;
	BOOLEAN			langflag;			/* ISO 639 language code present */
	BOOLEAN			langflag_2;			/* 2nd ISO 639 language code for 1+1 */
	UINT32			language;			/* ISO 639 code (0 if not defined) */
	UINT32			language2;			/* "" */
} ac3AudioStreamDescriptor_t, *ac3AudioStreamDescriptorPtr_t;
		

/* Caption Service Descriptor */
typedef enum {
	cct_line21		= 0,
	cct_advanced	= 1
} cc_type_k;

typedef enum {
	l21f_field1		= 0,
	l21f_field2		= 1
} line21_field_k;

typedef struct captionService {
	UINT32			language;
	cc_type_k			cc_type;
	union {
		line21_field_k	line21_field;
		UINT8			caption_service_number;
	} cc_id;
	BOOLEAN			easy_reader;
	BOOLEAN			wide_aspect_ratio;
} captionService_t, *captionServicePtr_t;

typedef struct captionServiceDescriptor {
	UINT8				number_of_services;
	captionServicePtr_t	service;			/* array of size 'number_of_services' */
} captionServiceDescriptor_t, *captionServiceDescriptorPtr_t;


/* Content Advisory Descriptor */
typedef struct ratingDimension {
	UINT8				rating_dimension_j;
	UINT8				rating_value;
} ratingDimension_t, *ratingDimensionPtr_t;

typedef struct ratingRegion {
	UINT8				rating_region;
	UINT8				rated_dimensions;
	ratingDimensionPtr_t	dimension;			/* array of size 'rated_dimensions' */
	UINT16			rating_description_length;
	UINT8				*rating_description;
} ratingRegion_t, *ratingRegionPtr_t;
 
typedef struct contentAdvisoryDescriptor {
	UINT8				rating_region_count;
	ratingRegionPtr_t		rating_region;		/* array of size 'rating_region_count' */
} contentAdvisoryDescriptor_t, *contentAdvisoryDescriptorPtr_t;


/* Extended Channel Name Descriptor */
typedef struct extendedChannelNameDescriptor {
	UINT16			long_channel_name_length;
	UINT8				*long_channel_name;
} extendedChannelNameDescriptor_t, *extendedChannelNameDescriptorPtr_t;


/* Service Location Descriptor */
typedef struct slElement {
	UINT8				stream_type;
	UINT16			elementary_PID;
	UINT32			ISO639_language_code;
} slElement_t, *slElementPtr_t;

typedef struct serviceLocationDescriptor {
	UINT16			PCR_PID;
	UINT8				number_elements;
	slElementPtr_t		element;			/* array of size 'number_elements' */
} serviceLocationDescriptor_t, *serviceLocationDescriptorPtr_t;


/* Component Name Descriptor */
typedef struct componentNameDescriptor {
	UINT16			component_name_length;
	UINT8				*component_name;
} componentNameDescriptor_t, *componentNameDescriptorPtr_t;




/********************************************************************
   Support for TVGOS stream

********************************************************************/

/*
 * DVB descriptor types
 */

#define DVB_VBI_data_tag     0x45


/*
The VBI data descriptor shall be used in the PSI PMT of a stream which carries VBI data as defined in
EN 301 775 [16]. The appropriate ES_info_field of the program map section describing a VBI data stream shall contain
one and only one VBI_data_descriptor.
For transmission of multiple VBI data streams in one service a VBI data descriptor in each of the component loops of
the program map section is required. */

typedef enum {
	ebu_teletext = 0x01, // EBU teletext (Requires additional teletext_descriptor)
	inverted_teletext = 0x02, // inverted teletext
	vps = 0x04, // VPS
	wss = 0x05, // WSS
	closed_captioning = 0x06, // Closed Captioning
	monochrome_422_samples = 0x07, // monochrome 4:2:2 samples

	// SCTE 127
	vitc = 0xf7,
	protected_3 = 0xf8,
	copy_protection = 0xf9,
	protected_2 = 0xfa,
	tvg2x = 0xfb,
	nabts = 0xfc,
	protected_1 = 0xfd,
	amol_48_96 = 0xfe,
} data_service_id_k;

#define VBIDataServiceIdString(t) ( \
	(t)==ebu_teletext ? "ebu_teletext" : \
	(t)==inverted_teletext ? "inverted_teletext" : \
	(t)==vps ? "vps" : \
	(t)==wss ? "wss" : \
	(t)==closed_captioning ? "closed_captioning" : \
	(t)==monochrome_422_samples ? "monochrome_422_samples" : \
	(t)==vitc ? "vitc" : \
	(t)==protected_3 ? "protected_3" : \
	(t)==copy_protection ? "copy_protection" : \
	(t)==protected_2 ? "protected_2" : \
	(t)==tvg2x ? "tvg2x" : \
	(t)==nabts ? "nabts" : \
	(t)==protected_1 ? "protected_1" : \
	(t)==amol_48_96 ? "amol_48_96" : \
	"?")

#define IS_VALID_DATA_SERVICE_ID(t) \
	((t)==0x01 || (t)==0x02 || (t)==0x04 || (t)==0x05 || (t)==0x06 || (t)==0x07 || \
	((t)>=0xf7 && (t)<=0xfe))


struct data_service_desc_t {
	UINT8 
		line_offset: 5,
		field_parity: 1;
};

typedef struct VBIDataServiceInfo_t {
	data_service_id_k data_service_id;
	/* This 8-bit field identifies a VBI service type contained in the elementary stream.
	A VBI service may temporarily not be present in the VBI data stream while its data_service_id still is present in the VBI_data_descriptor.
	This discrepancy may last no longer than 10 seconds. */
	UINT8 data_service_desc_len;
	struct data_service_desc_t *data_service_desc;
	/* This 5-bit field specifies the line number on which the associated coded data is intended to be presented if
	it is transcoded into the VBI. */

	#define DVB_VBI_DATA_PREALLOC_SIZE 16
	UINT8 prealloc_buf[DVB_VBI_DATA_PREALLOC_SIZE];
	// if data_service_desc_len <= DVB_VBI_DATA_PREALLOC_SIZE, 
	//   use this buffer instead of alloc dynamically.

} VBIDataServiceInfo;

typedef struct VBIDataDescriptor_t {
	UINT8 num_service_info;
	VBIDataServiceInfo *service_info;
} VBIDataDescriptor;


#ifdef __cplusplus
};
#endif

#endif // __DHL_PSI_TYPES_H__

